const app = getApp()
Page({
  data: {
    title:'',
    url:'',
    free:'',
    csId: '',
    sectionTitle:'',
    courseCode:'',
    menuList:[]

   
   },

//导航，点击的链接点
chooseCourse:function(e){
  //console.log("select:"+e.currentTarget.dataset.id)
  var id=e.currentTarget.dataset.id - 1
    this.setData({
       title: this.data.menuList[id].title,
       url:this.data.menuList[id].url,
       free:this.data.menuList[id].free
    })
  // console.log("免费:"+this.data.free)

    if (this.data.free) {
      //free，直接打开视频
      wx.navigateTo({
        url: "/pages/videoPlay/videoPlay?url=" + this.data.url + "&title=" + this.data.title,
      })

    } else {
      if (app.globalData.openid == "") {  
        //没有登录
        wx.showModal({
            title: '抱歉！',
            content: '登录之后，才能为您服务',
            showCancel: true, //是否显示取消按钮-----》false去掉取消按钮
            confirmText: "登录",    //默认是“确定”
            confirmColor: 'skyblue', //确定文字的颜色
            success(res) {
              //console.log('openid' + app.globalData.openId)
              if (res.confirm) {
                  wx.navigateTo({
                      url: '/pages/login/login'
                  })
              } 
            }  //success
         })
      } else {
       //此处非free，又已登录
          this.callVideoPlay()   
      }

    }  //free
    
 },

 callVideoPlay:function(){   
   //同步数学授权
   if (app.globalData.selectCourse == 'sxhs'
     || app.globalData.selectCourse == 'sxjh') 
     {
     if (app.globalData.member_zh_shuxue) {
       app.globalData.permission = '1'
     }
   }
   //同步物理授权
   if (app.globalData.selectCourse == 'wldx'
     || app.globalData.selectCourse == 'wlsy') 
     {
     if (app.globalData.member_zh_wuli) {
       app.globalData.permission = '1'
     }
   }
   //同步化学授权
   if (app.globalData.selectCourse == 'hxzh'
     || app.globalData.selectCourse == 'hxsy') 
     {
     if (app.globalData.member_zh_huaxue) {
       app.globalData.permission = '1'
     }
   }

   if (app.globalData.permission) {
     //授权之后，才打开视频
     wx.navigateTo({
       url: "/pages/videoPlay/videoPlay?url=" + this.data.url + "&title=" + this.data.title,
     })
   } else {
     //提示应授权
     wx.showModal({
       title: '抱歉！',
       content: '尚未取得授权，请前往授权',
       showCancel: true, //是否显示取消按钮-----》false去掉取消按钮
       confirmText: "前往授权",    //默认是“确定”
       confirmColor: 'skyblue', //确定文字的颜色
       success(res) {
         if (res.confirm) {
           wx.navigateTo({
             url: '/pages/payment/payment'
           })
         }
       }  //success
     })
   } //提示授权结束

 
 },

  //此无用
  widgetsToggle: function (e) 
  {
    var id = e.currentTarget.id, menuList = this.data.menuList;  
    for (var i = 0, len = menuList.length; i < len; ++i) {
      var recno = i;  
      if (menuList[i].chaptId == id) {
        menuList[i].open = !menuList[i].open;
      } else {
        menuList[i].open = false;
      }
    }

    this.setData({
      menuList:menuList,
      current_sectionId: this.data.menuList[recno].sectionRec[recno].sectionTitle
    });
  // console.log('resno:'+recno)
  },

  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab == e.target.dataset.current) {
      return false;

    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  //获到菜单标题文字
  getMenu:function(csId,courseCode){
    var that = this
    var p = new Promise(function (resolve, reject) {
      wx.request({
        url: 'https://www.beicho.com/get_videoList.php', //接口地址 
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        data: {
          scId: csId,
          courseCode:courseCode 
        },
        success: res => {
          console.log('courseList数据库获到的:', res.data)
          // console.log('courseList数据库获到的标题:', res.data[0].free)
          that.setData({
             menuList: res.data
          })

          //成功执行之后，返回
         // console.log('执行完成')
          resolve('success')
        }

      })

    })   //promise
    return p
   },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      csId: options.csId,
      courseCode:options.courseCode,
      sectionTitle:options.sectionTitle  

    })

    //console.log('courseList_课程编号:' + this.data.courseCode);
    //console.log('courseList_拿到节号:' + this.data.csId);
   
    //此节号用于去数据检索本节内容
    var that=this
    that.getMenu(options.csId, options.courseCode).then(function (p) {
      
      //console.log(p);
      //后面可以用传过来的数据做些其他操作
     // console.log('列表:', that.data.menuList)
     // console.log('列表中:',that.data.menuList[0].sectionRec[0].sectionTitle)

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})
